<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="title" v-model="visible" align-center width="800px" :z-index="2000" @close="handleClose">
    <el-form>
      <el-form-item label="审核">
        <el-radio-group v-model="form.audit_result" class="ml-4">
          <el-radio :value="1">通过</el-radio>
          <el-radio :value="2">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="驳回原因">
        <el-input v-model="form.audit_remark" type="textarea" :autosize="{minRows: 5}"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

const emit = defineEmits(['close','success'])

const form = ref({})
const title = ref()
const selection = ref()
const success = ref(false)

// 打开弹窗
const open = async (checkeds,t) => {
  if (checkeds && !checkeds.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if([...new Set(checkeds.map(d => d.audit_status))].length > 1) {
    ElMessage.warning('只能选择相同审核状态的数据!')
    return
  }
  selection.value = checkeds
  success.value = false
  title.value = t
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
  success.value && emit('success')
}
// 提交
const submit = () => {
  loadings.submit = true
  form.value.s_ids = selection.value.map(d => d.s_id)
  form.value.audit_status = selection.value[0].audit_status
  api.batchAudit(form.value).then(res => {
    ElMessage.success('操作成功')
    success.value = true
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>